/*
 * ListContentView.fx
 *
 * Created on Apr 12, 2010, 4:15:18 PM
 */

package AlertSystem;

import javafx.scene.Node;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import java.lang.Exception;
import java.sql.ResultSet;
import javafx.scene.text.Text;
import javafx.scene.control.ScrollView;
import javafx.scene.layout.LayoutInfo;

/**
 * @author quang
 */


public class ListContentWindow extends Window {

    
    var timeWindowWidth: Number = 180;
    var textWindowWidth = bind screenWidth - timeWindowWidth - 10;
    //10 = space btw time window and text window

//////////////////// PUBLIC VARIABLES ///////////////////

    public var windowHeight = screenHeight - windowContentYOffset;
    
    public var dataTableName: String;
    public var patientId: String;
    public var dateTimeFieldName: String;

    public var textFieldName: String;
    public var hasTextWindow: Boolean = true;

    //////////////////// PRIVATE VARIABLES //////////////////

    var dateTimeSeries: String[];
    var textSeries: Text[];

    //////////////////// FUNCTIONS //////////////////////////
    
    function getDataFromDB(patientId: String) {
        var rs: ResultSet;
        
        try {
            rs = DBConnection.query(connection, "SELECT {textFieldName}, {dateTimeFieldName} FROM {dataTableName} WHERE patientId = '{patientId}' ORDER BY {dateTimeFieldName} DESC");

            if (hasTextWindow) {
                 while(rs.next()){
                    insert rs.getString({dateTimeFieldName}) into dateTimeSeries;                    
                    insert Text {
                        translateY: 15
                        content: rs.getString({textFieldName})
                        wrappingWidth: textWindowWidth
                    } into textSeries;
                }//end while loop
            }
            else //hasTextWindow == false
            {
                timeWindowWidth = screenWidth;
                while (rs.next()) {                    
                    insert "{rs.getString({dateTimeFieldName})}         {rs.getString({textFieldName})}"
                    into dateTimeSeries;
                }//end while loop
            }            
        } catch(ex : Exception) {
            ex.printStackTrace();
        }         
    }

    protected override function create():Node {

//        this.getDataFromDB(patientId);

        def listView = ListView {
            items: dateTimeSeries//for (i in [0..rsSize]) dateTimeSeries[i]
            layoutInfo: LayoutInfo {
                width: timeWindowWidth
                height: windowHeight
            }
        }
        return HBox {
            translateY: windowContentYOffset
            spacing: 40
            content:
                [
                    listView,
                    ScrollView {
                        node: bind textSeries[listView.selectedIndex]
                        layoutInfo: LayoutInfo {
                            width: textWindowWidth
                            height: windowHeight
                        }
                    }                    
                ]
        }
    }

}
